var cartain = document.getElementById("cartain");
var escape1 = document.querySelector(".escape1");
var mark = document.querySelector(".mark");
var usedtime = document.querySelector(".usedtime"); 
class Game{
    //构造方法初始化成员变量
    constructor(){
        //时间
        this.time = 60;
        //打击数
        this.numTotal = 0;
        //难度
        this.level = this.getByid("difficult").value;
        //定时器（控制时间）
        this.timeOut = null;
         // 打击音效
         this.sound = this.sound.bind(this);
    }
    //获取标签id的函数
    getByid=(id)=>{
        return document.getElementById(id);
    }
    //执行函数（开始游戏 控制时间 结束游戏）
    playa=()=>{
        clearInterval(this.timeOut);
        this.timeOut = setInterval(()=>{
            this.time--;
            if(this.time==0){
                this.end()
            }
            this.getByid("time").innerHTML=this.time;
            this.getRandom()
        },1000)
    }
    //地鼠的生成和出现
    getRandom=()=>{
        var nums = this.getByid("mouseFrame").children.length;
        var red = parseInt(Math.random()*nums);
        var curMouse =  this.getByid("mouseFrame").children[red];
        curMouse.className = "mouse out";
        this.level = this.getByid("difficult").value;
        setTimeout(()=>{
            curMouse.className = "mouse";
            console.log(this.level);
        },this.level)
        
    }
    //打击事件 计算打击数
    hitRandom = ()=>{
        var mouseFrame = this.getByid("mouseFrame");
        mouseFrame.addEventListener("click",(ev)=>{
            switch(this.numTotal){
                case 10:
                    boss_cre();
                    break;
                case 30:
                    boss_cre();
                    break;
                case 50:
                    boss_cre();
                    break;
                case 70:
                    boss_cre();
                    break;
                case 80:
                    boss_cre();
                    break;
               
            }
            var target = ev.target;
            var clsName = target.className;
            if(clsName.indexOf("out")!=-1){
                target.className = "mouse hit"
                this.getByid("numHit").innerHTML=++this.numTotal;
            }
        })
    }
    //游戏结束时执行的函数
    end=()=>{
        mark.innerHTML = this.numTotal*100;
        usedtime.innerHTML = 60-this.time
         clearInterval(this.timeOut);
         cartain.style.display = "block"
    }
    //打击音效
    sound(){
        var audio = new Audio('../打地鼠image/audio.mp3');
        audio.play();
    }
}

//实例化对象
let game=new Game();
//开始按钮事件
function start(){
    playMusic()
    game.playa()
    game.hitRandom()
}
//结束按钮事件
function end(){
    game.end()
}
function sound(){
   game.sound()
}

//背景音乐
var audio = document.getElementById('backgroundMusic');
function playMusic() {
    
    audio.play();
    audio.loop = true;
}


//美化弹窗
//点击退出事件
escape1.onclick = ()=>{
    cartain.style.display = 'none'
    game.time = game.getByid("time").innerHTML=60;
    game.numTotal = game.getByid("numHit").innerHTML=0;
    location.assign(location.href)
}


//地鼠王
var boss1 = document.getElementById("boss");
var boss1_img = boss1.querySelector("img")
var p = boss1.querySelector("p");
//血量
var blood =10;
boss1_img.onclick = ()=>{
    game.sound()
    blood--;
    if(blood==0){
        game.numTotal+=10
        game.getByid("numHit").innerHTML=game.numTotal;
        boss_away();
    }
    p.className = "ani"
    setTimeout(()=>{
        p.className = ""
    },50)
}
 //生成boss
function boss_cre(){
    //初始化血量
    blood = 10;
    boss1.style.display = "block";
    boss1.classList.add("appearboss")
}
//消灭boss
function boss_away(){
    boss1.style.display = "none";
    boss1.classList.remove("appearboss")
}

